package design.simdi.code.final_paper.utils;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import design.simdi.code.final_paper.site.pages.BasePage;

/**
 * 
 * @author Marius Dumitriu - (Jun 22, 2010 - 8:24:20 PM)
 * 
 */
public abstract class YesNoModalWindow extends ModalWindow {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private YesNoModalWindowPanel yesNoModalWindowPanel;
	private ModalResult modalResult;
	private String intrebare;

	public YesNoModalWindow(String id, String intrebare) {
		super(id);

		this.setIntrebare(intrebare);
		init();
	}

	public YesNoModalWindow(String id, IModel<?> model, String intrebare) {
		super(id, model);

		this.setIntrebare(intrebare);
		init();
	}

	public void init() {
		yesNoModalWindowPanel = new YesNoModalWindowPanel(getContentId(), YesNoModalWindow.this) {

			private static final long serialVersionUID = 1L;

			@Override
			public void onOK(AjaxRequestTarget target) {
				onYes(target);
				close(target);
			}

			@Override
			public void onCancel(AjaxRequestTarget target) {
				onNo(target);
				close(target);

			}
		};
		yesNoModalWindowPanel.setOutputMarkupPlaceholderTag(true);
		yesNoModalWindowPanel.get(YesNoModalWindowPanel.CONTENT).replaceWith(
				new Label(YesNoModalWindowPanel.CONTENT, new ResourceModel(getIntrebare())));

		setContent(yesNoModalWindowPanel);
		setTitle(new ResourceModel("titlu"));
		setOutputMarkupPlaceholderTag(true);
		setModalResult(ModalResult.CANCEL);
		setInitialHeight(140);
		setInitialWidth(460);

		setCloseButtonCallback(new ModalWindow.CloseButtonCallback() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public boolean onCloseButtonClicked(AjaxRequestTarget target) {
				setModalResult(ModalResult.CANCEL);
				return true;
			}
		});

		setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void onClose(AjaxRequestTarget target) {
				switch (getModalResult()) {
				case CANCEL:
					Page page = YesNoModalWindow.this.getParent().getPage();
					target.addComponent(page.get(BasePage.ERRORS_PANEL));
					warn(page.getString("operation.canceled"));
					break;

				case OK:

					Page pag = YesNoModalWindow.this.getParent().getPage();
					target.addComponent(pag.get("content"));
					target.addComponent(pag.get(BasePage.ERRORS_PANEL));
					info(pag.getString("operation.success"));

					break;
				}

			}
		});

	}

	public void setModalResult(ModalResult modalResult) {
		this.modalResult = modalResult;
	}

	public ModalResult getModalResult() {
		return modalResult;
	}

	public void setYesNoModalWindowPanel(YesNoModalWindowPanel yesNoModalWindowPanel) {
		this.yesNoModalWindowPanel = yesNoModalWindowPanel;
	}

	public YesNoModalWindowPanel getYesNoModalWindowPanel() {
		return yesNoModalWindowPanel;
	}

	public abstract void onYes(AjaxRequestTarget target);

	public abstract void onNo(AjaxRequestTarget target);

	public void setIntrebare(String intrebare) {
		this.intrebare = intrebare;
	}

	public String getIntrebare() {
		return intrebare;
	}

}
